package com.ltu.model.response.base;

import java.util.List;

import org.springframework.data.domain.Page;

import com.github.pagehelper.PageInfo;
import com.ltu.util.common.StrUtils;

public class PageData<T> {

	private Long totalElements;
	
	private Integer pageNumber;

	private Integer pageSize;
	
	private boolean last;
	
	private boolean first;
	
	private List<T> content;
	
	
	
	public boolean isLast() {
		return last;
	}

	public void setLast(boolean last) {
		this.last = last;
	}

	public boolean isFirst() {
		return first;
	}

	public void setFirst(boolean first) {
		this.first = first;
	}
	
	public Long getTotalElements() {
		return totalElements;
	}

	public void setTotalElements(Long totalElements) {
		this.totalElements = totalElements;
	}

	public List<T> getContent() {
		return content;
	}

	public void setContent(List<T> content) {
		this.content = content;
	}

	public Integer getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(Integer pageNumber) {
		this.pageNumber = pageNumber;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	
	public static <T> PageData<T> valueOfpageData(PageInfo<T> pageInfo){
		PageData<T> page = new PageData<T>();
		page.setContent(pageInfo.getList());
		page.setPageNumber(pageInfo.getPageNum());
		page.setPageSize(pageInfo.getPageSize());
		page.setTotalElements(pageInfo.getTotal());
		setFirstAndLast(pageInfo.getTotal(), pageInfo.getPageNum(), pageInfo.getPageSize(), page);
/*		page.setFirst(pageInfo.getFirstPage()==0?false:true);
		page.setLast(pageInfo.getLastPage()==0?false:true);*/
		//可以继续赋值、、、、
		return page;
	}
	
	public static <T> PageData<T> setFirstAndLast(long total,Integer pageNum, Integer pageSize, PageData<T> page) {
		if (!StrUtils.isVaileNum(total)) {
			page.setFirst(true);
			page.setLast(true);
		}else{
			if (total<=pageSize) {
				page.setFirst(true);
				page.setLast(true);
			}else{
				long pageCount = total/pageSize;
				long isCount = total%pageSize;
				if (isCount > 0) {
					pageCount = pageCount + 1;
				}
				if (pageNum==1) {
					page.setFirst(true);
					page.setLast(false);
				}
				if (pageNum > 1 && pageNum < pageCount) {
					page.setFirst(false);
					page.setLast(false);
				}
				if (pageNum == pageCount) {
					page.setFirst(false);
					page.setLast(true);
				}
			}
		}
		return page;
	}
	
	public static <T> PageData<T> valueOfpageData(Page<T> pageInfo){
		PageData<T> page = new PageData<T>();
		page.setContent(pageInfo.getContent());
		page.setPageNumber(pageInfo.getNumber());
		page.setPageSize(pageInfo.getSize());
		page.setTotalElements(pageInfo.getTotalElements());
		page.setFirst(pageInfo.isFirst());
		page.setLast(pageInfo.isLast());
		//可以继续赋值、、、、
		return page;
	}

	public static <T> PageData<T> valueOfpageData(Page<T> pageInfo,List<T> list){
		PageData<T> page = new PageData<T>();
		page.setContent(list);
		page.setPageNumber(pageInfo.getNumber());
		page.setPageSize(pageInfo.getSize());
		page.setTotalElements(pageInfo.getTotalElements());
		page.setFirst(pageInfo.isFirst());
		page.setLast(pageInfo.isLast());
		//可以继续赋值、、、、
		return page;
	}
}